package cn.jasonone.example;

import cn.hutool.core.util.RandomUtil;
import cn.jasonone.jame.GameScene;
import cn.jasonone.jame.JameApplication;
import cn.jasonone.jame.basic.GameContext;
import cn.jasonone.jame.basic.KeyEventHandler;
import cn.jasonone.jame.model.GLabel;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Font;
import javafx.util.Duration;

import java.util.Iterator;
import java.util.List;

/**
 * 事件监听案例
 * 键盘打字游戏
 */
public class EventExample extends JameApplication {
    private final String basicString="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,/\"[]{}-=_+!@#$%^&*()";
    //分数
    private int score=0;
    @Override
    public void init(GameContext gc) {
        this.setSize(300,500);
        //创建场景
        String sceneId = this.createGameScene();
        //创建记录分数的控件
        Label score=new Label("分数: 0");
        score.setPrefWidth(100);
        score.setPrefHeight(30);
        score.setStyle("-fx-text-fill: aqua");
        score.setFont(Font.font(24));
        score.setLayoutY(0);
        //根据场景ID,获得场景
        GameScene scene = this.getGameScene(sceneId);
        scene.appendRight(score,false);
        //创建时间轴
        Timeline timeline=new Timeline();
        //定义关键帧: 创建文字标签,每隔50ms创建一次
        KeyFrame createLabelFrame = new KeyFrame(Duration.millis(100), e->{
            //随机生成字符串
            String str = RandomUtil.randomString(basicString, 1);
            //创建标签
            GLabel label=new GLabel(str);
            //移动标签,横坐标随机,纵坐标在屏幕之外
            label.moveTo(RandomUtil.randomInt((int) (getSize().getWidth()-label.getWidth())),-label.getHeight());
            //将标签添加到场景中
            scene.append(label);
        });
        //定义关键帧: 移动文字标签,每隔30ms移动一次
        KeyFrame moveLabelFrame = new KeyFrame(Duration.millis(30), e->{
            //获得所有标签: 此方法只能获取精灵无法获取控件
            List<GLabel> spirits = scene.getSpirits(GLabel.class);
            if (spirits != null) {
                Iterator<GLabel> iterator = spirits.iterator();
                while(iterator.hasNext()){
                    GLabel label = iterator.next();
                    //将精灵向下移动2个像素
                    label.move(0,2);
                    //判断标签是否移动到场景之外
                    if(!scene.getRect().isCollision(label)){
                        //如果移动到场景之外,则游戏结束
                        timeline.stop();
                        Alert alert=new Alert(Alert.AlertType.CONFIRMATION);
                        alert.setTitle("温馨提示");
                        alert.setHeaderText("游戏结束");
                        alert.setContentText("你的最终分数为:"+EventExample.this.score);
                        //显示对话框
                        alert.initOwner(scene.getWindow());
                        alert.show();
                    }
                }
            }
        });
        timeline.getKeyFrames().addAll(createLabelFrame,moveLabelFrame);
        //设置时间轴的循环次数为无限循环
        timeline.setCycleCount(Timeline.INDEFINITE);
        //播放时间轴
        timeline.play();
        scene.addKeyListener("1", new KeyEventHandler() {
            @Override
            public void keyTyped(KeyEvent e) {
                //接收键盘字符
                String c=e.getCharacter();
                //比对场景中的文字标签
                List<GLabel> labels = scene.getSpirits(GLabel.class);
                Iterator<GLabel> iterator = labels.iterator();
                while (iterator.hasNext()){
                    GLabel label = iterator.next();
                    if(label.getText().equals(c)){
                        EventExample.this.score+=1;
                        score.setText("分数:"+EventExample.this.score);
                        label.remove();
                    }
                }
            }
        });
        this.showGameScene(sceneId);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
